package org.cc.cloud;


import java.util.List;
import java.util.Random;

import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.Server;

public class MyRibbonRule implements IRule {

	ILoadBalancer lb;
	public Server choose(Object arg0) {

		
		Random ran=new Random();
		int r=ran.nextInt(10);
		List<Server> services =lb.getAllServers();
		//会自动检查规则中的服务注册者是否健康。如8082服务器挂掉了就算设置了这个规则也不会执行
		if(r>7){
			System.out.println("******进入自定义规则8081"+"******"); 
			return getServerByPort(services,8081);
		}
		else{
			System.out.println("******进入自定义规则8082"+"******"); 
			return getServerByPort(services,8082);
		}
		 
		

	}
   // public  <T> T getServerByPort(List<T> services,int point){
	public Server getServerByPort(List<Server> services,int point){
    	for(Server s:services){
    		if(s.getPort()==point){
    			return s;
    		}
    	}
    	return null;
    	
    }
	public ILoadBalancer getLoadBalancer() {
		// TODO Auto-generated method stub
		return lb;
	}

	public void setLoadBalancer(ILoadBalancer arg0) {
		lb=arg0;
		// TODO Auto-generated method stub

	}

}
